//
//  RollingLabel.m
//  BMProject
//
//  Created by chris on 15/11/2.
//  Copyright © 2015年 MengHuan. All rights reserved.
//

#import "RollingLabel.h"
@interface RollingLabel ()
{
    NSTimer * timer;
    UIScrollView *showView;
    CGSize size;
    UILabel *label;
    NSArray * arr;
    
}
//@property(nonatomic , assign)int m;
@end
@implementation RollingLabel
@synthesize num;
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        
    }
    return self;
}
- (void)initWithArray:(NSArray *)array
{

    [timer invalidate];
    timer = nil;

    // 获取文本
//    NSDictionary * dic = [array objectAtIndex:0];
//    NSString * nameStr = [self transformName:dic[@"realName"]];
//    NSString * numStr = dic[@"amount"];
    
    NSString *string = [array objectAtIndex:0];
    arr = array;
    num = 0;
    // 初始化label
    label	  = [UILabel new];
    label.text		  = string;
    label.numberOfLines = 0;
    label.textColor	 = [UIColor lightGrayColor];
    label.font		  = [UIFont systemFontOfSize:14];

    // 计算尺寸
    size		 = [label.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    label.frame		 = CGRectMake(10, 0, DEF_SCREEN_WIDTH-20, 20);
    //    label.backgroundColor = [UIColor lightGrayColor];
    
    // 初始化ScrollView
    showView = \
    [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, size.height)];
    showView.contentSize   = size;
    showView.showsHorizontalScrollIndicator = NO;
    [showView addSubview:label];
    [self addSubview:showView];
    
    // 形成边缘的遮罩
    UIImageView *imageView = \
    [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, size.height)];
    imageView.image = [UIImage imageNamed:@"bg"];
    [self addSubview:imageView];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}

- (void)animation
{
    //NSLog(@"one = %d",num);
    num++;
    //NSLog(@"two = %d",num);
    if (num < arr.count)
    {
        [UIView animateKeyframesWithDuration:1.0
                                       delay:0
                                     options:0
                                  animations:^{
                                      // 计算移动的距离
                                      CGPoint point = showView.contentOffset;
                                      point.y = size.height;
                                      showView.contentOffset = point;
                                  }
                                  completion:^(BOOL finished) {
                                      CGPoint point = showView.contentOffset;
                                      point.y = 0;
                                      showView.contentOffset = point;
//                                      NSDictionary * dic = [arr objectAtIndex:num];
//                                      NSString * nameStr = [self transformName:dic[@"realName"]];
//                                      NSString * numStr = dic[@"amount"];
                                     // NSLog(@"three = %d",num);
                                      NSString *string = nil;
                                      if (num == -1)
                                      {
                                         string = [arr objectAtIndex:0];
                                      }
                                      else
                                      {
                                          string = [arr objectAtIndex:num];
                                      }

                                      label.text = string;
                                  }];
    }
    else
    {
        num = -1;
    }
    
}
- (void)start
{
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}
- (void)stop
{
    [timer invalidate];
    timer = nil;
}
//姓名转换
- (NSString *)transformName:(NSString *)name
{
    NSMutableArray * tempArr = [NSMutableArray array];
    for (int i = 0; i < name.length-1; i++)
    {
        [tempArr addObject:@"*"];
    }
    NSString * starStr = [tempArr componentsJoinedByString:@""];
    name =  [name stringByReplacingCharactersInRange:NSMakeRange(1, name.length-1) withString:starStr];
    return name;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
